<?php

namespace app\apisell\controller\admin;

use app\apisell\model\groupLink\GroupLinkModel;
use app\apisell\service\GroupLink\GroupLinkService;
use app\BaseController;
use app\common\utils\Res;
use DateTime;
use think\facade\Db;

class GroupLink extends BaseController
{
    /**
     * 团购链接列表
     */
    public function getList()
    {
        $param = $this->request->param();
        $param['uid'] =  $this->request->uid;
        $level = $this->request->popuLevel;
        $is_popu = $this->request->isPopu;
        $result = GroupLinkService::getList($param,$level,$is_popu);
        return Res::ok($result);
    }
    
    /**
     * 创建团购链接
     */
    public function createGroupLink()
    {
        $param = $this->request->param();
        $param['add_id'] =  $this->request->uid; //登录的后台账户ID，不是推广ID
        $param['popu_id'] =  $this->request->popuId; //账号绑定的推广员ID
        
        if(!$param['goods_id']){
            return Res::error('请选择商品！');
        }
        
        if($param['people_num'] < 2){
            return Res::error('成团人数必须大于2位，请重新输入数量！');
        }
        
        if(empty($param['group_apply_time'])){
            return Res::error('开始结束时间不能为空！');
        }
        
        $timeDiff = $this->timeDiff($param['group_apply_time']);
        if($param['group_hour'] > $timeDiff){
            return Res::error('开始结束时间不能大于成团时间！');
        }
        
        foreach ($param['goods_spec'] as $v){
            if (!is_numeric($v['commission'])) {
                return Res::error('规格名称为：' . $v['name'] . '的佣金不是正确的格式，请重新设置！');
            }
            if($v['commission'] > $v['goods_commission']){
                return Res::error('规格名称为：' . $v['name'] . '的佣金不能大于商品设置的总佣金，请重新设置！');
            }
        }
        
        $result = GroupLinkService::createGroupLink($param);
        return Res::ok($result);
    }
    
    /**
     * 生成链接和二维码
     */
    public function checkGroupLink()
    {
        $param = $this->request->param();
        //判断链接是否过期
        $res = Db::table('xh_sell_groups_link')
            ->where('id',$param['id'])
            ->where('end_time','>',date('Y-m-d H:i:s',time()))
            ->find();
        if(!$res){
            return Res::error('该链接已过期，请重新创建链接！');
        }
        $param['popu_user_id'] =  $this->request->uid;
        $result = GroupLinkService::checkGroupLink($param);
        
        $result['is_popu'] = $this->request->isPopu;
        
        return Res::ok($result);
    }

    /**
     * 查询当前推广链接
     */
    public function viewGroupLink()
    {
        $param = $this->request->param();
        $result = GroupLinkService::viewGroupLink($param);
        return Res::ok($result);
    }

    /**
     * 编辑推广链接
     */
    public function editeGroupLink()
    {
        $param = $this->request->param();
        $param['add_id'] =  $this->request->uid; //登录的后台账户ID，不是推广ID
        
        $param['link_level'] = $this->request->popuLevel;
        
        $result = GroupLinkService::editeGroupLink($param);
        return Res::ok($result);
    }
    
    /**
     * 修改状态
     */
    public function editeLinkStatus()
    {
        $param = $this->request->param();
        $param['uid'] =  $this->request->uid;
        $result = GroupLinkService::editeLinkStatus($param);
        return Res::ok($result);
    }

    /**
     * 获取商品
     */
    public function getGoodsList()
    {
        $param = $this->request->param();
        $result = GroupLinkService::getGoodsList($param);
        return Res::ok($result);
    }
    
    //获取当前登录的信息
    public function getLoginInfo()
    {
        if($this->request->isPopu == 1){
            $result['popu_level'] = 0;
        }else{
            $result = Db::table('xh_sell_popu_user')
                ->where('member_id',$this->request->uid)
                ->find();
        }
        $result['is_popu'] = $this->request->isPopu;
        return Res::ok($result);
    }
    
    //计算日期时间之差(小时)
    private function timeDiff($date)
    {
        // 两个日期时间字符串
        $datetime1 = $date[0];
        $datetime2 = $date[1];

        // 创建DateTime对象
        $date1 = new DateTime($datetime1);
        $date2 = new DateTime($datetime2);

        // 计算时间差
        $interval = $date2->diff($date1);

        // 获取相差的小时数
        $hours = $interval->h + ($interval->d * 24);
        
        return $hours;
    }
    
}